#include "wdog.h"
#include "ui_wdog.h"
#include <QtWidgets/QGridLayout>
#include <QPushButton>
#include <QtCore>


#define COLUMN_SPACING 20

WDog::WDog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WDog)
{
    SetMainBacklight();

    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool | Qt::WindowDoesNotAcceptFocus);

    ui->setupUi(this);
    ycapi = new Ycapi();




    wdttimer=new QTimer(this);
    connect(wdttimer,SIGNAL(timeout()),this,SLOT(on_wdtTimerUpdate()));
    ui->wdogTimeout->setText(QString::fromLocal8Bit("10"));
    wdtRun=0;
}
void WDog::on_wdtTimerUpdate()
{
     bool ok;
     char buf[10];
     QString str= ui->wdogTimeout->text();
     sprintf(buf,"%d",str.toInt(&ok,10)-1);
     ui->wdogTimeout->setText(QString::fromLocal8Bit(buf));
}
void WDog::SetMainBacklight()
{
QPalette bgpal = palette();
bgpal.setColor (QPalette::Background, QColor (0, 0 , 0, 255));
bgpal.setColor (QPalette::Foreground, QColor (255,0,0,255));
setPalette (bgpal);
}
WDog::~WDog()
{
    delete ui;
}

void WDog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void WDog::on_start_clicked()
{
    bool ok;
    QString str= ui->wdogTimeout->text();
    if(wdtRun==0)
    {
        ycapi->SetWDog(str.toInt(&ok,10));
        ycapi->StartWDog();
        wdttimer->start(1000);
        ui->start->setText(QString::fromLocal8Bit("Feed"));
        ui->wdogTimeout->setEnabled(false);
        wdtRun=1;
    }
    else
    {
        ycapi->FeedWDog();
        ui->wdogTimeout->setText(QString::fromLocal8Bit("10"));
    }
}

void WDog::on_stop_clicked()
{
    ycapi->StopWDog();
    wdttimer->stop();
    wdtRun=0;
    ui->start->setText(QString::fromLocal8Bit("Start"));
    ui->wdogTimeout->setEnabled(true);
    ui->wdogTimeout->setText(QString::fromLocal8Bit("10"));
}

void WDog::on_close_clicked()
{
    this->close();
}

